using Game.XH;
using UnityEngine;

namespace Game.Component.Character
{
	public class Hand : CharacterComponent
	{
		public enum Dir
		{
			Left = 0,
			Right = 1
		}

		public enum State
		{
			Default = 0,
			Take = 1,
			LieLeft = 2,
			LieRight = 3
		}

		private const float MOVE_SPEED = 180f;

		private const float POWER_FACTOR = 20f;

		private const float DECELERATION = 140f;

		private const float FOLLOW_ITEM_MOVE_SPEED = 90f;

		private const float FOLLOW_ITEM_ANGLE = 50f;

		private const float TAKE_UPPER_ANGELE = 30f;

		private const float TAKE_LOWER_ANGELE = 80f;

		private const float NORMAL_UPPER_ANGLE = 30f;

		public Dir dir;

		public State state;

		public Transform takePoint;

		public int clothingId;

		public int bodyId;

		public Transform upperArm;

		private Transform upperArmClothing;

		public Transform lowerArm;

		public Transform lowerArmClothing;

		private float eachUpAngle;

		private float upAngle;

		private float eachDownAngle;

		private float downAngle;

		private float upperAngle;

		private float normalUpperAngle = 30f;

		private float minUpperAngle = 10f;

		private float maxUpperAngle = 84f;

		public float targetDistY;

		public BaseItem targetItem;

		public BaseItem takeItem;

		public Hand(Transform trans, Dir dir)
			: base(trans)
		{
			this.dir = dir;
			upperAngle = normalUpperAngle;
			upperArm = trans.Find("UpperArm");
			upperArm.localEulerAngles = new Vector3(0f, 0f, upperAngle);
			upperArm.SetLocalZ(-1E-05f);
			upperArmClothing = trans.Find("UpperArm/Clothing");
			upperArmClothing.SetLocalZ(-2E-05f);
			lowerArm = trans.Find("UpperArm/LowerArm");
			lowerArm.SetLocalZ(-1E-05f);
			lowerArmClothing = trans.Find("UpperArm/LowerArm/Clothing");
			lowerArmClothing.SetLocalZ(-2E-05f);
			takePoint = trans.Find("UpperArm/LowerArm/TakePoint");
			takePoint.localEulerAngles = new Vector3(0f, 0f, 110f);
		}

		public void SetBody(int id, Transform upperCopy, Transform lowerCopy, Color32 color)
		{
			if (bodyId != id)
			{
				bodyId = id;
				ChangeSprite(upperArm, upperCopy);
				ChangeSprite(lowerArm, lowerCopy);
				SetColor(color);
			}
		}

		public void SetColor(Color32 color)
		{
			ChangeSpriteColor(upperArm, color);
			ChangeSpriteColor(lowerArm, color);
		}

		public void SetClothing(int id, Transform upperCopy, Transform lowerCopy)
		{
			if (clothingId != id)
			{
				clothingId = id;
				ChangeClothing(upperArmClothing, upperCopy);
				ChangeClothing(lowerArmClothing, lowerCopy);
			}
		}

		public void SetLowerArmClothingVisible(bool val)
		{
			lowerArmClothing.SetObjectActive(val);
		}

		public void SetBoxColliderEnabled(bool val)
		{
			if (lowerArm != null)
			{
				BoxCollider2D component = lowerArm.GetComponent<BoxCollider2D>();
				if (component != null)
				{
					component.enabled = val;
				}
			}
		}

		private void ChangeClothing(Transform trans, Transform copy)
		{
			SpriteRenderer component = trans.Find("Sprite").GetComponent<SpriteRenderer>();
			if (copy != null)
			{
				SpriteRenderer component2 = copy.GetComponent<SpriteRenderer>();
				if (component2 != null)
				{
					component.sprite = component2.sprite;
					component.transform.localPosition = component2.transform.localPosition;
					return;
				}
			}
			component.sprite = null;
		}

		public void SetTakeItem(BaseItem item)
		{
			takeItem = item;
			if (item == null || item.itemData.holdType == 1)
			{
				ChangeState(State.Default);
			}
			else
			{
				ChangeState(State.Take);
			}
		}

		public void ChangeState(State val)
		{
			if (state == val)
			{
				return;
			}
			if (state == State.Default)
			{
				state = val;
				if (state == State.Take)
				{
					ChangeToTakeState();
				}
				else if (state == State.LieLeft)
				{
					if (dir == Dir.Left)
					{
						SetUpperAngle(90f);
						lowerArm.localEulerAngles = new Vector3(0f, 0f, 100f);
						lowerArm.localScale = new Vector3(1f, 1f, 1f);
					}
					else
					{
						SetUpperAngle(20f);
						lowerArm.localEulerAngles = new Vector3(0f, 0f, 0f);
					}
				}
				else if (state == State.LieRight)
				{
					if (dir == Dir.Left)
					{
						SetUpperAngle(20f);
						lowerArm.localEulerAngles = new Vector3(0f, 0f, 0f);
					}
					else
					{
						SetUpperAngle(90f);
						lowerArm.localEulerAngles = new Vector3(0f, 0f, 100f);
						lowerArm.localScale = new Vector3(1f, 1f, 1f);
					}
				}
			}
			else if (state != State.Take || !(takeItem != null))
			{
				state = val;
				if (val == State.Default)
				{
					lowerArm.localEulerAngles = new Vector3(0f, 0f, 0f);
					lowerArm.localScale = new Vector3(1f, 1f, 1f);
					normalUpperAngle = 30f;
					minUpperAngle = 20f;
					maxUpperAngle = 84f;
				}
				else if (state == State.Take)
				{
					ChangeToTakeState();
				}
			}
		}

		private void ChangeToTakeState()
		{
			lowerArm.localEulerAngles = new Vector3(0f, 0f, 80f);
			lowerArm.localScale = new Vector3(-1f, 1f, 1f);
			normalUpperAngle = 30f;
			minUpperAngle = 20f;
			maxUpperAngle = 60f;
		}

		private void SetUpperAngle(float angle)
		{
			upperAngle = angle;
			upperArm.localEulerAngles = new Vector3(0f, 0f, angle);
		}

		public void LateUpdate()
		{
			if (state != 0 && state != State.Take)
			{
				return;
			}
			if (targetItem != null)
			{
				if (upperAngle == 50f)
				{
					return;
				}
				eachUpAngle = 90f * Time.deltaTime;
				if (upperAngle > 50f)
				{
					upperAngle -= eachUpAngle;
					if (upperAngle <= 50f)
					{
						upperAngle = 50f;
					}
				}
				else if (upperAngle < 50f)
				{
					upperAngle += eachUpAngle;
					if (upperAngle >= 50f)
					{
						upperAngle = 50f;
					}
				}
				upperArm.localEulerAngles = new Vector3(0f, 0f, upperAngle);
			}
			else if (downAngle > 0f)
			{
				eachDownAngle = 180f * Time.deltaTime;
				downAngle -= eachDownAngle;
				upperAngle -= eachDownAngle;
				if (upperAngle <= minUpperAngle)
				{
					upperAngle = minUpperAngle;
					upAngle = 0f;
					downAngle = 0f;
				}
				upperArm.localEulerAngles = new Vector3(0f, 0f, upperAngle);
			}
			else if (upAngle > 0f)
			{
				eachUpAngle = 180f * Time.deltaTime;
				upAngle -= eachUpAngle;
				upperAngle += eachUpAngle;
				if (upperAngle >= maxUpperAngle)
				{
					upperAngle = maxUpperAngle;
					upAngle = 0f;
				}
				else if (upAngle <= 0f)
				{
					upAngle = 0f;
					downAngle = 0f;
				}
				upperArm.localEulerAngles = new Vector3(0f, 0f, upperAngle);
			}
			else if (upperAngle != normalUpperAngle)
			{
				if (Mathf.Abs(upperAngle - normalUpperAngle) < 0.3f)
				{
					upperAngle = normalUpperAngle;
				}
				else
				{
					upperAngle = Mathf.Lerp(upperAngle, normalUpperAngle, Time.deltaTime * 3f);
				}
				upperArm.localEulerAngles = new Vector3(0f, 0f, upperAngle);
			}
		}

		public void AddUpPower(float val)
		{
			if (downAngle > 0f)
			{
				downAngle -= val;
			}
			if (downAngle <= 0f)
			{
				downAngle = 0f;
				upAngle += val * 20f;
			}
		}

		public void AddDownPower(float val)
		{
			upAngle = 0f;
			downAngle += val * 20f;
		}
	}
}
